package acm.蓝桥杯二分前缀;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 打地鼠 {
    static int[][] matrix;
    static int N;   //行数
    static int M;   //列数

    public static void main(String[] args) {
        Reader sc = new Reader();
        N = sc.nextInt();
        M = sc.nextInt();
        matrix = new int[N][M];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }
        System.out.println(fun());
    }

//    static {
//        matrix = new int[][]{
//                {1,0,1},
////                {2, 4, 2},
////                {1, 2, 1},
//        };
//        N=matrix.length;
//        M=matrix[0].length;
//        System.out.println(fun());
//        System.out.println(checkR(2));
//        System.out.println(checkC(2));
//    }

    public static int fun() {
        int r = M;
        int rCount;
        while ((rCount = checkR(r)) == -1) {
            r--;
        }
        int c = N;
        int cCount;
        while ((cCount = checkC(c)) == -1) {
            c--;
        }
        return rCount / c;
    }

    /**
     * 检测行的宽度为n时，是否符合
     */
    public static int checkR(int n) {
        int res = 0;
        for (int[] m : matrix) {
            int[] clone = m.clone();
            int i = 0;
            for (; i <= M - n; i++) {
                res += clone[i];  // 至少要敲击的次数（左上角点）
                for (int j = i + 1; j < i + n; j++) {
                    clone[j] -= clone[i];
                    if (clone[j] < 0) {
                        return -1;
                    }
                }
            }
            for (; i < clone.length; i++) { //末尾是否还有残留列
                if (clone[i] != 0) return -1;
            }
        }
        return res;
    }

    /**
     * 检测列的长度为n时是否符合
     */
    public static int checkC(int n) {
        int res = 0;
        int[][] copy = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                copy[i][j] = matrix[i][j];
            }
        }
        for (int j = 0; j < M; j++) {   //列
            int i = 0;
            for (; i <= N - n; i++) {   //行
                res += copy[i][j];
                for (int k = i + 1; k < i + n; k++) {
                    copy[k][j] -= copy[i][j];
                    if (copy[k][j] < 0) {
                        return -1;
                    }
                }
            }
            for (; i < N; i++) {
                if (copy[i][j] != 0) return -1;
            }
        }
        return res;
    }

    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public void clear() {
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
